package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.common.LoginVo;
import org.example.common.R;
import org.example.common.StatusCode;
import org.example.mapper.PostsMapper;
import org.example.mapper.PostsMapper;
import org.example.pojo.entity.Posts;
import org.example.pojo.entity.SysUser;
import org.example.pojo.vo.PostVo;
import org.example.service.PostService;
import org.example.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static org.example.utils.securityContextUserInfo.getContextUserInfo;

@Service
public class PostServiceImpl extends ServiceImpl<PostsMapper, Posts>
        implements PostService {
    @Autowired
    SysUserService sysUserService;

    @Override
    public Page<PostVo> pageList(Integer page, Integer size, String title, String delFlag) {
        Page<Posts> pages = new Page<>(page, size);
        LambdaQueryWrapper<Posts> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNoneBlank(title)) {
            wrapper.like(Posts::getTitle, title);
        }
        if (StringUtils.isNoneBlank(delFlag)) {
            wrapper.like(Posts::getDelFlag, delFlag);
        }
        List<PostVo> PostsVoList = new ArrayList<>();
        Page<Posts> PostsPage = baseMapper.selectPage(pages, wrapper);
        PostsPage.getRecords().forEach(Posts -> {
            SysUser user = sysUserService.getById(Posts.getCreateId());
            String userName = user.getUserName();
            PostVo PostsVo = new PostVo();
            BeanUtils.copyProperties(Posts, PostsVo);
            PostsVo.setUserName(userName);
            PostsVoList.add(PostsVo);
        });
        Page<PostVo> PostsVoPage = new Page<>();
        PostsVoPage.setRecords(PostsVoList);
        PostsVoPage.setPages(PostsPage.getPages());
        PostsVoPage.setSize(PostsPage.getSize());
        PostsVoPage.setTotal(PostsPage.getTotal());
        return PostsVoPage;

    }

    @Override
    public R getPostDetail(String scenicId) {
        return R.ok(this.getById(scenicId)) ;
    }

    @Override
    public R addPost(Posts post) {
        LoginVo userInfo = getContextUserInfo();
        post.setCreateTime(LocalDateTime.now());
        post.setUpdateTime(LocalDateTime.now());
        post.setDelFlag("0");
        post.setType(1);//帖子标识1
        post.setUpdateId(Integer.valueOf(userInfo.getUserId()));
        post.setCreateId(Integer.valueOf(userInfo.getUserId()));
        //保存景点基本信息
        this.save(post);
        return R.ok("操作成功");
    }
    @Override
    public R updatePost(Posts Posts) {
        /*LambdaQueryWrapper<Posts> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Posts::getScenicName, Posts.getScenicName());
        Posts PostsOne = this.getOne(wrapper);
        if (PostsOne != null) {
            return R.error(StatusCode.ATTR_ALREADY_EXIST);
        }*/
        LoginVo userInfo = getContextUserInfo();
        Posts.setUpdateTime(LocalDateTime.now());
        Posts.setUpdateId(Integer.valueOf(userInfo.getUserId()));
        //更新景点基本信息
        this.updateById(Posts);
        return R.ok("操作成功");
    }

    @Override
    public void removePostById(String id) {
        Posts post = new Posts();
        post.setDelFlag("1");
        post.setId(Integer.valueOf(id));
        this.updateById(post);
    }
}
